//LiteXLoader Dev Helper
/// <reference path="c:\Users\stranger\.vscode\extensions\moxicat.lxldevhelper-0.1.4/Library/JS/Api.js" /> 


//随机物品         史莱姆方块 面包   蛋糕     海绵     火药   苹果     熟牛肉        小麦种子    紫颂果         箱子     烈焰粉         书     萤石粉         蜂巢         熟猪排            钻石原矿        铁锭     绿宝石     附魔台
const randomItem=["slime","bread","cake","sponge","gunpowder","apple","cooked_beef","wheat_seeds","chorus_fruit","chest","blaze_powder","book","glowstone_dust","beehive","cooked_porkchop","diamond_ore","iron_ingot","emerald","enchanting_table",
//                     红石      铁块         附魔苹果           烈焰棒       绿宝石块         经验瓶               海晶灯      恶魂眼泪      钻石        龙息            末影水晶
                    "redstone","iron_block","appleenchanted","blazed_rod","emerald_block","experience_bottle","sealantern","ghast_tear","diamond","dragon_breath","end_crystal", 
//                  末影之眼     钻石块            幻翼膜            潜影盒        复活图腾  潮涌核心      龙蛋     鞘翅 
                    "ender_eye","diamond_block","phantom_membrane","shulker_box","tetem","conduit","dragon_egg","elytra","light_block"];
//随机减益
const deBuff=["bad_omen","blindness","fatal_poison","hunger","instant_damage","mining_fatigue","nausea","poison","slowness","weakness",
                "wither","slowness"];
//随机增益
const buff=["absorption","conduit_power","fire_resistance","haste","health_boost","instant_health","invisibility","jump_boost","levitation","night_vision",
            "regeneration","resistance","saturation","slow_falling","speed","strength","village_hero","water_breathing"];
         

mc.listen("onServerStarted",function(){
    logger.setConsole(true);
    logger.info("[HalfIsland]>>>>>>>>>>>>>> finished 盲盒插件已加载!");
})

mc.listen("onUseItemOn",function(player,item,block,side){//开盲盒
    if(player&&item&&block){
        if(!player.hasTag("blind")){
            selectForWin10(player);
            if(item.name==="§g便§5携§b盲§4盒"){
                let form=mc.newCustomForm();
                form=form.setTitle("§g便§5携§b盲§4盒");
                form=form.addLabel("随身便携盲盒，可以随时随地的开盲盒。");
                form=form.addInput("§6输入投注金额,金额§4越大§6,中奖几率§4越高。");
    
                player.sendForm(form,function(player,data){
                    if(player&&data){
                        
                        let data1=parseInt(data[1]);
                        let reg=new RegExp("^\\d+$");//正则验证正整数
                        if(reg.test(data1&&data1!=="0")){
                            reward(player,data1);
                            let sum=money.get(player.xuid)-data1;
                            money.set(player.xuid,sum);
    
                        }else{
                            title("输入非法数字!",player);
                        }
                        
                    }
                })
                
            }
        }
        
    }
})

function reward(player,data){//奖励
    
    let id=randomId();

    switch(id){
        case 0:{//抽钱
            let add=randomAddMoney(data);
            let sum=money.get(player.xuid)+add;
            money.set(player.xuid,sum);
            if (sum>0)
                title(`获得 ${add.toString()}金钱`,player);
            else
                title(`再来一次`,player);    
            }break;
        case 1:{//抽物品
            if(data>500){
                let item=randomItems(data);
                if (item){
                    title(`获得物品 ${item.name} 数量 ${item.count}`,player);
                    player.giveItem(item);
                }
                    
                player.refreshItems();
            }
            else{
                title(`再来一次`,player);
            }
        }break;
        case 2:{//buff
            if(data>2000){
                let dOrB=Math.random();
                let buffName=dOrB>=0.5?randomBuff():randomDeBuff();
                let level=randomLevel();
                let time=randomTime();

                mc.runcmdEx(`effect ${player.name} ${buffName} ${time.toString()} ${level.toString()}`);
                title(`获得效果 ${buffName} 时间${time.toString()} 强度${level.toString()}`,player);
            }else{
                title(`再来一次`,player);
            }
            
        }
    }
}

function randomId(){
    let id=(Math.random()*3);

    if(id<1.5){
        id=0;
    }else if(id<=2.5){
        id=1;
    }
    else if(id<=3){
        id=2;
    }
    return id;
}

function randomItems(data){//随机物品
    
    let base=0;
    let count=randomCount();
    let index=0;


    if(data>0&&data<10000){//0-9
        index=Math.floor(data/1000);
        base=0;
    }
    else if(data>=10000&&data<50000){//0-19
        data-=20000;
        index=Math.floor(data/4000);
        base=10;
    }
    else if(data>=50000&&data<100000){//20-29
        data-=50000;
        index=Math.floor(data/5000);
        base=20;
    }
    else if(data>=100000&&data<200000){//30-34;
        data-=100000;
        index=Math.floor(data/20000);
        base=30;
    }
    else if(data>=200000){//35-39;
        data-=200000;
        index=Math.floor(data/100000);

        if(index>4)
            index=4;

        base=35;
    }

    item=randomItem[base+(Math.floor(Math.random()*index))];

    while(!item){//物品为空时
        item=mc.newItem(randomItem[Math.floor(Math.random()*(base+index))],count);
    }

    return item;
}

function randomAddMoney(data){//随机金钱
    let rate=0;//利率
    let base=0;//基础利率

    if(data>0&&data<50000){
        base=0.35;
        rate=1.45;
    }
    else if(data>=50000&&data<100000){
        base=0.45;
        rate=1.65;
    }
    else if(data>=100000&&data<200000){
        base=0.65;
        rate=1.85;
    }
    else if(data>=200000){
        base=0.65;
        rate=2.15;
    }

    let min=Math.random()*base;
    let max=Math.random()*rate;

    let money=Math.floor(data*min)+Math.floor(data*max);

    if(money===0){
        money=200;
    }

    return money;
}

function randomDeBuff(){
    return deBuff[Math.floor(Math.random()*(deBuff.length-1))];
}

function randomBuff(){
    return buff[Math.floor(Math.random()*(buff.length-1))];
}

function randomLevel(data){//随机等级

    if(data<=1000){
        return 2;
    }
    else if(data<=4000){
        return 3;
    }
    else if(data<=10000){
        return 4;
    }else{
        return 5;
    }
}

function randomCount(data){//随机物品数量

    let base=0;
    let rand=0;
    if(data>0&&data<10000){//4-36
        base=4;
        rand=Math.floor(Math.random()*32);
    }
    else if(data>=10000&&data<100000){//12-60
        base=12;
        rand=Math.floor(Math.random()*48);
    }
    else if(data>=100000){//24-64
        base=24;
        rand=Math.floor(Math.random()*48);
    }
   
    let num=base+Math.floor(Math.random()*rand);

    return num;
}

function randomTime(data){//随机buff时间

    let max=(data-10000)/200000*1200;//后40取决于大款
    let min=data/10000*120;

    let res=min+max;
    
    return res>0?res:10;
}

function title(data,player){
    mc.runcmdEx(`title ${player.name} actionbar §6${data}`);
}
function selectForWin10(player){
    player.addTag("blind");//加标签

    setInterval(()=>{//异步删除标签
        player.removeTag("blind");
    },200);
}